import Blockly from 'blockly/core'
import 'blockly/python_compressed'
import 'blockly/blockly_compressed'
import 'blockly/blocks_compressed'

Blockly.Blocks.try_judgement = {
  init: function () {
    this.appendDummyInput().appendField('try')
    this.appendStatementInput('try').setCheck(null)
    this.appendDummyInput()
      .appendField('except')
      .appendField(
        new Blockly.FieldDropdown([
          ['BaseException', 'BaseException'],
          ['Exception', 'Exception'],
          ['ArithmeticError', 'ArithmeticError'],
          ['OverflowError', 'OverflowError'],
          ['IOError', 'IOError'],
          ['ImportError', 'ImportError'],
          ['NameError', 'NameError'],
          ['RuntimeError', 'RuntimeError'],
          ['ValueError', 'ValueError']
        ]),
        'exceptions'
      )
    this.appendStatementInput('except').setCheck(null)
    this.appendDummyInput().appendField('else')
    this.appendStatementInput('else').setCheck(null)
    this.setPreviousStatement(true, null)
    this.setNextStatement(true, null)
    this.setColour(230)
    this.setTooltip('')
    this.setHelpUrl('')
  }
}

Blockly.Python.try_judgement = function (block) {
  const statementsTry = Blockly.Python.statementToCode(block, 'try')
  const dropdownExceptions = block.getFieldValue('exceptions')
  const statementsExcept = Blockly.Python.statementToCode(block, 'except')
  const statementsElse = Blockly.Python.statementToCode(block, 'else')
  const code =
    'try :' +
    '\n' +
    statementsTry +
    '\n' +
    'except : ' +
    dropdownExceptions +
    '\n' +
    statementsExcept +
    '\n' +
    'else :' +
    '\n' +
    statementsElse
  return code
}

export default Blockly
